/**
 * Created by hanj on 2020/11/11 19:55
 */
package com.example.demo;

import com.google.common.collect.Lists;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 导出Excel测试Controller
 *
 * @author caoju
 */
@RestController
public class ExcelExportController {

    /**
     * 导出Excel测试
     */
    @RequestMapping(value = "/exportExcelTest", method = RequestMethod.GET)
    public void exportExcelTest(HttpServletResponse response) {
        List<User> personList = this.findUser();
        ExcelUtils.exportExcel(ExcelUtils.getWorkbook("用户信息列表", "用户信息", User.class, personList), "用户信息列表导出", response);
    }

    /**
     * 自定义格式导出Excel测试
     *
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcelTest1", method = RequestMethod.GET)
    public String export(HttpServletResponse response) throws Exception {
        // 文件名
        String title = "单点登录信息";
        // 设置标题
        String[] headers = {"id", "姓名", "性别", "创建时间"};
        String[] headers2 = {"id", "姓名", "性别", "编号", "创建时间"};
        // 获取数据库数据
        List<User> personList = findUser();
        List<Teacher> teacher = findTeacher();
        ExcelUtils.fillExcelData(personList, teacher, headers, headers2, title, response);
        return null;
    }


    /**
     * 模拟数据库查询返回用户信息
     */
    private List<User> findUser() {
        List<User> list = Lists.newArrayList();
        User user = new User();
        user.setId(10);
        user.setName("张三");
        user.setSex(1);
        user.setCreateTime(new Date());
        list.add(user);
        User user1 = new User();
        user1.setId(26);
        user1.setName("李四");
        user1.setSex(1);
        user1.setCreateTime(new Date());
        list.add(user1);
        User user2 = new User();
        user2.setId(28);
        user2.setName("王丽");
        user2.setSex(2);
        user2.setCreateTime(new Date());
        list.add(user2);
        return list;
    }

    /**
     * 模拟数据库查询返回教师信息
     */
    private List<Teacher> findTeacher() {
        List<Teacher> list = Lists.newArrayList();
        Teacher teacher = new Teacher();
        teacher.setId(11);
        teacher.setName("张三");
        teacher.setSex(1);
        teacher.setNo("1231");
        teacher.setCreateTime(new Date());
        list.add(teacher);
        Teacher teacher1 = new Teacher();
        teacher1.setId(25);
        teacher1.setName("李四");
        teacher1.setSex(1);
        teacher1.setNo("1231");
        teacher1.setCreateTime(new Date());
        list.add(teacher1);
        Teacher teacher2 = new Teacher();
        teacher2.setId(10);
        teacher2.setName("晨晨");
        teacher2.setSex(1);
        teacher2.setNo("1231");
        teacher2.setCreateTime(new Date());
        list.add(teacher2);
        Teacher teacher3 = new Teacher();
        teacher3.setId(25);
        teacher3.setName("晨子");
        teacher3.setSex(1);
        teacher3.setNo("1231");
        teacher3.setCreateTime(new Date());
        list.add(teacher3);
        return list;
    }

}
